# ------------------------------------------------------------------------------
# Makes all targets for 05_cost_effectiveness
# Author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bsub -q big -R "rusage[mem=10000]" make
# ------------------------------------------------------------------------------

# Config -----------------------------------------------------------------------
.ONESHELL:
include config.mk

# Locals -----------------------------------------------------------------------
# can override with make OVERNIGHT = TRUE
# overnight option refers only to plots produced by script 01;
# you'll need to edit scripts manually to get overnight stats/tables from 02/03
OVERNIGHT = FALSE

# Recipes ----------------------------------------------------------------------
## all			: Builds all cost-effectiveness targets
.PHONY: all
all: cost_curves cost_stats cost_tbls mace_v_yield_plot output_fgs output_tbls

## prep			: Prepares cohort for score diagnostics
.PHONY: prep
prep: $(COHORT)
$(COHORT): ../02_prep_and_summarize_cohort/
	$(MAKE) -C ../02_prep_and_summarize_cohort

## cost_curves		: Plots cost-effectiveness curves
.PHONY: cost_curves
cost_curves: $(COST_CURVES)
$(COST_CURVES) &: 01_daly_cutoff_analysis.sh scripts/01_daly_cutoff_analysis.R \
$(COHORT)
	bash $< $(OVERNIGHT)

## cost_stats		: Reports various cost-effectiveness stats
.PHONY: cost_stats
cost_stats: $(COST_STATS)
$(COST_STATS): 02_cost_effectiveness_stats.sh \
scripts/02_cost_effectiveness_stats.R $(COHORT)
	bash $<

## cost_tbls		: Produces cost-effectiveness latex tables
.PHONY: cost_tbls
cost_tbls: $(COST_TBLS)
$(COST_TBLS): 03_yield_costeff_table.sh \
scripts/03_yield_costeff_table.R $(COHORT)
	bash $<

## mace_v_yield_plot	: Plots mace-untested vs yield-tested for risk tile
.PHONY: mace_v_yield_plot
mace_v_yield_plot: $(MACE_V_YIELD_PLOT)
$(MACE_V_YIELD_PLOT): 04_mace_vs_yield.sh scripts/04_mace_vs_yield.R $(COHORT) \
$(DIRS) $(COST_STATS)
	bash $<

## output_tbls		: Copies tables used in paper to `output` directory
.PHONY: output_tbls
output_tbls: $(OUTPUT_TBLS)
$(OUTPUT_DIR)/tables/%.tex : %.tex
	cp $< $@

## output_fgs		: Copies tables used in paper to `output` directory
.PHONY: output_fgs
output_fgs: $(OUTPUT_FGS)
$(OUTPUT_DIR)/figures/%.png : %.png
	cp $< $@

# Helper Recipes ---------------------------------------------------------------
## dirs			: Creates temp,log directories for storing output
.PHONY: dirs
dirs: $(DIRS)
%/.f :
	mkdir -p $(dir $@)
	touch $@

## style			: Styles R scripts according to tidyverse guidelines
.PHONY: style
style:
	source ~/anaconda3/etc/profile.d/conda.sh
	conda activate stressr
	R --vanilla --silent -e "styler::style_dir(path = 'scripts', filetype = c('R', 'Rmd', 'Rprofile'))"

## clean			: Clears log and temp dirs
.PHONY: clean
clean:
	rm -rf temp/* log/*

## help			: Prints available .PHONY targets with description
.PHONY : help
help : Makefile
	@sed -n 's/^##//p' $<
